import { LEVEL_CONFIG, calculateLevelXP } from '../config/gameConfig'

export const levelSystem = {
  currentLevel: 1,
  currentXP: 0,
  nextLevelXp: 100,
  levelUpCallback: null,

  initialize(level, xp, callback) {
    this.currentLevel = level
    this.currentXP = xp
    this.levelUpCallback = callback
    this.nextLevelXp = this.calculateNextLevelXp()
  },

  addXP(amount) {
    this.currentXP += amount
    
    while (this.currentXP >= this.nextLevelXp) {
      this.levelUp()
    }
  },

  levelUp() {
    this.currentLevel++
    this.currentXP -= this.nextLevelXp
    this.nextLevelXp = this.calculateNextLevelXp()
    
    if (this.levelUpCallback) {
      const rewards = {
        coinBonus: this.currentLevel * 100
      }
      this.levelUpCallback(this.currentLevel, rewards)
    }
  },

  calculateNextLevelXp() {
    return Math.floor(100 * Math.pow(1.5, this.currentLevel - 1))
  },

  getLevelProgress() {
    return (this.currentXP / this.nextLevelXp) * 100
  }
} 